package edu.hdu.android.sudoku;


import android.util.Log;

class Game {

    private static final String TAG = "Game";

    private String str;
    char[] fin;
    public int[] sudoku;
    private int[][][] used = new int[9][9][];
    
    Game() {
        if ("".equals(GameActivity.loadStr) || GameActivity.loadStr == null ||
                MyApplication.flag != MyApplication.difficulty+1 ) {
            Log.d(TAG, "Game: 1");
            str = GameActivity.str;
            fin = str.toCharArray();
        } else {
            Log.d(TAG, "Game: 2");
            fin = GameActivity.str.toCharArray();
            str = GameActivity.loadStr;
        }
        sudoku = fromPuzzleString(str);
        MyApplication.sudoku = sudoku;
        calculateAllUsedNum();
    }

    //获取九宫格指定位置某个数字对应字符
    String getNumString(int x, int y) {
        if (getNum(x, y) == 0) return "";
        else return String.valueOf(getNum(x, y));
    }

    boolean setNumIfValid(int x, int y, int value) {
        if (value != 0) {
            int[] nums = getUsedNum(x, y);
            for (int num : nums) {
                if (num == value) return false;
            }
        }
        setNum(x, y, value);
        calculateAllUsedNum();
        return true;
    }

    int[] getUsedNum(int x, int y) {
        return used[x][y] = calculateUsedNum(x, y);
    }

    //获取九宫格中指定位置的某个数字
    private int getNum(int x, int y) {
        return sudoku[y*9 + x];
    }

    //设置九宫格中指定位置的某个数字
    private void setNum(int x, int y, int num) {
        sudoku[y*9 + x] = num;
        MyApplication.sudoku = sudoku;
    }

    //计算所有单元格不可能数据
    private void calculateAllUsedNum() {
        for (int x=0; x<9; x++) {
            for(int y=0; y<9; y++) {
                used[x][y] = calculateUsedNum(x, y);
            }
        }
    }

    //计算某个单元格不可能数据
    private int[] calculateUsedNum(int x, int y) {
        int[] c = new int[9];

        if (fin[y*9+x] != '0') {
            return new int[]{0,1,2,3,4,5,6,7,8,9};
        }

        for (int i=0; i<9; i++) {
            if (i == y) continue;
            int t = getNum(x, i);
            if (t != 0) c[t-1] = t;
        }
        for (int i=0; i<9; i++) {
            if (i == x) continue;
            int t = getNum(i, y);
            if (t != 0) c[t-1] = t;
        }

        int startx = (x/3)*3;
        int starty = (y/3)*3;
        for (int i=startx; i<startx+3; i++) {
            for (int j=starty; j<starty+3; j++) {
                if (i == x && j == y) continue;
                int t = getNum(i, j);
                if (t != 0) {
                    c[t-1] = t;
                }
            }
        }

        int numOfUsed = 0;
        for (int t : c) {
            if (t != 0) numOfUsed++;
        }
        int[] c1 = new int[numOfUsed];
        numOfUsed = 0;
        for (int t : c) {
            if (t != 0) {
                c1[numOfUsed++] = t;
            }
        }
        return c1;
    }

    //根据字符串数组初始化数据
    private int[] fromPuzzleString(String str) {
        int[] sudo = new int[str.length()];
        for (int i=0; i<sudo.length; i++) {
            sudo[i] = str.charAt(i) - '0';
        }
        return sudo;
    }
}
